﻿namespace Pps.Gui.Base
{
    using System;
    using Microsoft.SPOT;
    using Pps.Gui.Properties;

    public abstract class DisplayObject : Rectangle, IDisposable
    {
        private bool enabled;
        private bool visible;
        private bool isDisposed;

        private Font font;

        private Color backgroundColor = Color.White;
        private Color foregroundColor = Color.Black;

        public DisplayObject()
            : this(0, 0, 0, 0)
        {
        }

        public DisplayObject(int x, int y, int width, int height)
            : this(new Point(x, y), new Size(width, height))
        {
        }

        public DisplayObject(Point position, Size size)
            : base(position, size)
        {
            this.font = Resources.GetFont(Resources.FontResources.Arial_32);

            this.isDisposed = false;

            this.enabled = true;
            this.visible = true;
        }

        public virtual bool Enabled
        {
            get { return this.enabled; }
            set
            {
                if (this.enabled != value)
                {
                    this.enabled = value;
                    this.Invalidate();
                }
            }
        }

        public virtual bool Visible
        {
            get { return this.visible; }
            set
            {
                if (this.visible != value)
                {
                    this.visible = value;
                    this.Invalidate();
                }
            }
        }

        public Font Font
        {
            get { return this.font; }
            set
            {
                if (this.font != value)
                {
                    this.font = value;
                    this.Invalidate();
                }
            }
        }

        public Color BackgroundColor
        {
            get { return this.backgroundColor; }
            set
            {
                if (this.backgroundColor != value)
                {
                    this.backgroundColor = value;
                    this.Invalidate();
                }
            }
        }

        public Color ForegroundColor
        {
            get { return this.foregroundColor; }
            set
            {
                if (this.foregroundColor != value)
                {
                    this.foregroundColor = value;
                    this.Invalidate();
                }
            }
        }

        public virtual void Invalidate()
        {
            this.Render();
        }

        internal virtual void Render()
        {
        }

        protected virtual void Paint(Graphics graphics)
        {
        }

        public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing == true && this.isDisposed == false)
            {
                this.isDisposed = true;
            }
        }
    }
}